import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//获取原型上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
        redirect: {
            name: 'leader'
        },
    },
    {
        path: '/survey',
        name: 'survey',
        component: () => import('../views/homePage'),
        redirect: {
            path: '/'
        },
        children: [
            {
                path: '/survey/leader',
                name: 'leader',
                component: () => import('../components/loader')
            }, {
                path: '/survey/question',
                name: 'question',
                component: () => import('../components/question')
            }, {
                path: '/survey/person',
                name: 'person',
                component: () => import('../components/person')
            },
            {
                path: '/survey/guide',
                name: 'guide',
                component: () => import('../components/guide')
            },
            {
                path: '/survey/addnew',
                name: 'addnew',
                component: () => import('../components/addnew'),
            },
            {
                path: '/survey/paper',
                name: 'paper',
                component: () => import('../components/paper'),
            },
            {
                path: '/survey/paperData/:id',
                name: 'paperData',
                component: () => import('../components/paperData'),
                children: [
                    {
                        path: '/survey/personData/dataView/',
                        name: 'dataView',
                        component: () => import('../components/dataView'),
                    },
                ]
            },
            {
                path: '/survey/template',
                name: 'template',
                component: () => import('../components/question_template'),
            },
            {
                path: '/survey/personData',
                name: 'personData',
                component: () => import('../components/personData'),
            },
            {
                path: '/survey/personReply',
                name: 'personReply',
                component: () => import('../components/personReply'),
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login')
    },
    {
        path: '/sign',
        name: 'sign',
        component: () => import('../views/register')
    },
    {
        path: '/answerSheet/:id',
        name: 'answerSheet',
        component: () => import('../components/answerSheet'),
    },
    /*{
      path: '/about',
      name: 'About',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/!* webpackChunkName: "about" *!/ '../views/About.vue')
    }*/
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
//挂载路由守卫导航
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪里来
    //next()  放行  next('/login') 强制跳转到login

    if (to.path == '/login') return next();
    if (to.path == '/sign') return next();
    if (to.path == '/survey/leader') return next();
    if (to.name == 'answerSheet') return next();
    if (to.name == 'question') return next();
    //获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})


export default router
